package com.extrace.loader;

import android.app.Activity;
import android.util.Log;

import com.extrace.model.PackageHistory;
import com.extrace.model.TransHistory;
import com.extrace.model.TransPackageContent;
import com.extrace.net.HttpAsyncTask;
import com.extrace.net.HttpResponseParam;
import com.extrace.net.IDataAdapter;
import com.extrace.net.JsonUtils;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

public class PackageHistoryLoader extends HttpAsyncTask {

    String url;
    public List<TransPackageContent> list;
    public TransHistory th;
    private Activity context;
    private IDataAdapter<PackageHistory> adapter;

    public PackageHistoryLoader(IDataAdapter<PackageHistory> adapter, Activity context) {
        super(context);
        this.context = context;
        this.adapter = adapter;

        list = new ArrayList<TransPackageContent>();
        th = null;

        //url = "http://192.168.7.144:8080/TestCxfHibernate_war_exploded/REST/Domain/";
    }

    @Override
    public void onDataReceive(String class_name, String json_data) {
        if (class_name.equals("TransHistory")) {
            th = JsonUtils.fromJson(json_data, new TypeToken<TransHistory>(){});
        } else{
            list = JsonUtils.fromJson(json_data, new TypeToken<List<TransPackageContent>>(){});
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onStatusNotify(HttpResponseParam.RETURN_STATUS status, String str_response) {
        Log.i("onStatusNotify", "onStatusNotify: " + str_response);
    }


    public void getTPC(String id) {
        url = "http://192.168.6.79:8080/TestCxfHibernate_war_exploded/REST/Domain/";
        url += "getTransPackageContentList/" + id + "?_type=json";
        try {
            execute(url, "GET");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void getHistoryByPkgId(String id) {
        url = "http://192.168.6.79:8080/TestCxfHibernate_war_exploded/REST/Domain/";
        url += "getTransHistory/" + id + "?_type=json";
        try {
            execute(url, "GET");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
